package com.aliyun.frontclient;

import java.util.HashMap;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.os.Process;
import android.util.Log;
import android.view.Surface;
import android.view.TextureView;
import android.view.View;

public class FrontClientSurfaceTextureListener extends
        FrontClientSurfaceViewListener implements
        TextureView.SurfaceTextureListener {

    // TODO 服务端修正后可自动识别窗口大小，无需显示传入
    private static final String TAG = "FCSTL";

    // 为避免每次跨进程传递时创建新的Surface，因此开始时记录下来，以后直接拿来使用
    private HashMap<SurfaceTexture, Surface> mMap;

    // 用于标识外置摄像头，即后录
    protected TextureView mBackTextureView;

    public FrontClientSurfaceTextureListener(Context cnt) {
        super(cnt);
    }

    public FrontClientSurfaceTextureListener(View textureView) {
        super(textureView);
    }

    @Override
    protected void init(Context cnt, View surfaceView) {
        super.init(cnt, surfaceView);
        mMap = new HashMap<SurfaceTexture, Surface>(4);
    }

    public void setBackTextureView(TextureView textureView) {
        println("-setBackTextureView-" + textureView);
        mBackTextureView = textureView;
    }

    public SurfaceTexture getBackSurfaceTexture() {
        TextureView btv = mBackTextureView;
        if (btv != null
        // && btv instanceof btv
        ) {
            return btv.getSurfaceTexture();
        }
        return null;
    }

    private Surface makesureSurface(SurfaceTexture surfaceTexture) {
        HashMap<SurfaceTexture, Surface> map = mMap;
        Surface sss = map.get(surfaceTexture);
        if (sss == null) {
            sss = new Surface(surfaceTexture);
            //
            if (getBackSurfaceTexture() == surfaceTexture) {
                setBackSurface(sss);
            }
            map.put(surfaceTexture, sss);
        }
        return sss;
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture,
            int width, int height) {

        println("-onSurfaceTextureAvailable-" + surfaceTexture);
        Surface sss = makesureSurface(surfaceTexture);

        surfaceCreated(sss, width, height);
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture,
            int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
        println("-onSurfaceTextureDestroyed-" + surfaceTexture);
        if (mMap != null) {
            Surface sss = mMap.remove(surfaceTexture);
            surfaceDestroyed(sss);
        }
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // println("-onSurfaceTextureUpdated-"+surface );
        // Surface sss = makesureSurface( surface );
    }

    static void println(Object o) {
        Log.i(TAG, "pid:" + Process.myPid() + ", " + Thread.currentThread()
                + " : " + String.valueOf(o));
    }
}
